﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace footwear
{
    public partial class ManageProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DbConnect.Connection == null)
            {
                var dbConnect = new DbConnect();
            }
            var categoryBus = new CategoryBus();
            var list = categoryBus.GetListCategory();
            if (Request.QueryString["action"] != null && Request.QueryString["action"] != "")
            {
                var shoeInfoBus = new ShoeInfoBus();
                var item = shoeInfoBus.GetSpecificShoe(Request.QueryString["id"]);
                if (!Page.IsPostBack)
                {
                    txtCode.Text = item.Code;
                    txtName.Text = item.Name;
                    txtMainLink.Text = item.ImageSource;
                    txtPrice.Text = item.Price;
                    chkMenu.Checked = item.ShowIndex == 1;
                    txtBackLink.Text = item.ShoeSubInfo.BackImageSource;
                    txtColor.Text = item.ShoeSubInfo.Color;
                    txtDescription.Text = item.ShoeSubInfo.Description;
                    txtFrontLink.Text = item.ShoeSubInfo.FrontImageSource;
                    txtLeftLink.Text = item.ShoeSubInfo.LeftImageSource;
                    txtRightLink.Text = item.ShoeSubInfo.RightImageSource;
                    txtSize.Text = item.ShoeSubInfo.Size;
                    txtTopLink.Text = item.ShoeSubInfo.TopImageSource;
                    DropDownType.Items.Clear();
                    foreach (var category in list)
                    {
                        DropDownType.Items.Add(category.Type);
                    }
                    DropDownType.Text = item.ShoeSubInfo.Type;
                    Button1.Text = "Sửa";
                    txtCode.Enabled = false;
                }
            }
            else
            {
                if (Request.QueryString["action"] == "delete")
                {
                    var shoeInfoBus = new ShoeInfoBus();
                    shoeInfoBus.DeleteProductByCode(Request.QueryString["id"]);
                }
                Button1.Text = "Thêm";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtName.Text))
            {
                var shoeInfoBus = new ShoeInfoBus();
                var typeId = DropDownType.SelectedIndex + 1;
                var showMenu = "";
                if (chkMenu.Checked)
                {
                    showMenu = "1";
                }
                else
                {
                    showMenu = "0";
                }
                if (txtCode.Enabled)
                {
                    shoeInfoBus.AddProduct(txtCode.Text, txtName.Text, txtPrice.Text, txtMainLink.Text, showMenu,
                                           typeId.ToString(), txtSize.Text, txtColor.Text, txtDescription.Text,
                                           txtLeftLink.Text, txtRightLink.Text, txtFrontLink.Text, txtBackLink.Text,
                                           txtTopLink.Text);
                }
                else
                {
                    shoeInfoBus.UpdateProductByCode(txtCode.Text, txtName.Text, txtPrice.Text, txtMainLink.Text, showMenu,
                                           typeId.ToString(), txtSize.Text, txtColor.Text, txtDescription.Text,
                                           txtLeftLink.Text, txtRightLink.Text, txtFrontLink.Text, txtBackLink.Text,
                                           txtTopLink.Text);
                }
                txtCode.Text = "";
                txtName.Text = "";
                txtMainLink.Text = "";
                txtPrice.Text = "";
                txtBackLink.Text = "";
                txtColor.Text = "";
                txtDescription.Text = "";
                txtFrontLink.Text = "";
                txtLeftLink.Text = "";
                txtRightLink.Text = "";
                txtSize.Text = "";
                txtTopLink.Text = "";
                DropDownType.SelectedIndex = 0;
                chkMenu.Checked = false;
                txtCode.Enabled = true;
                Button1.Text = "Thêm";
            }
        }
    }
}